using System;
using System.Collections;
using System.Xml;
using System.Xml.XPath;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

//CID 0238 //2009.03.08 //Begin
namespace InfoJetSoft.Service.Util
{
	/// <summary>
	/// Summary description for ImportActionPrefixValue.
	/// </summary>
	public class ImportActionUtil
	{
		public static string getPrefixValue( string prefix, string prefixType, XmlNode itemNode, NamespaceResolver nsResovler )
		{	
			if( prefixType.Equals( "data" ) )
			{
				return prefix;
			}
			if( prefixType.Equals( "formula" ) )
			{
				if( prefix.StartsWith( "\"" ) && prefix.EndsWith( "\"" ) )
				{
					string prefixValue = prefix.Substring( 1, prefix.Length - 2 );
					return prefixValue;
				}
				else
				{
					XPathNavigator xpathNavi = itemNode.CreateNavigator();
					XPathExpression xpathExpr = xpathNavi.Compile( prefix );
					xpathExpr.SetContext( nsResovler );
					object result =  xpathNavi.Evaluate( xpathExpr );
					if( result is XPathNodeIterator )
					{
						XPathNodeIterator nodeIterator = (XPathNodeIterator)result;
						if(nodeIterator.MoveNext()) 
						{
							XmlNode currentNode = ((System.Xml.IHasXmlNode)nodeIterator.Current).GetNode();
							return DOMUtil.getNodeValue( currentNode );
						}
					}
					else
					{
						return result.ToString();
					}
				}
			}
			return null;
		}

		//CID 0238 //2009.03.09 //Begin
		public static string getSeparatorValue( string separator, string separatorType )
		{
			if( separatorType == null )
			{
				return null;
			}
			if( !separatorType.Equals( "0" ) )
			{
				if( separatorType.Equals( "5" ) )
				{
					return "\r\n";
				}
				else if( separatorType.Equals( "7" ) )
				{
					return "\r\n_______________\r\n\r\n";
				}
				else if( separatorType.Equals( "6" ) || separatorType.Equals( "8" ) )
				{
					return "\r\n\r\n";
				}
				else
				{
					return separator;
				}
			}
			return null;
		}
		//CID 0238 //2009.03.09 //End

		//CID 0238 //2009.03.09 //Begin
		public static string DefaultSeparator = "\r\n_______________\r\n\r\n";
		//CID 0238 //2009.03.09 //End

		//CID 0243 //2009.03.16 //Begin
		public static string getRelativePath( string path, string subPath )
		{
			if( subPath.StartsWith( path ) )
			{
				string relativePath = subPath.Substring( path.Length );
				if( relativePath.StartsWith( "/" ) )
				{
					return relativePath.Substring( 1 );
				}
			}
			return null;
		}
		//CID 0243 //2009.03.16 //End
	}
}
//CID 0238 //2009.03.08 //End